#! /usr/bin/env python3
import os

# Import Object
from .report import report


class PostExploit:
    # Perform post exploitation attacks
    def __init__(self):
        pass

    # Determine type of hash based on length and call the appropriate function
    def CrackingHashesType(self, hashfile, wordlist):
        self.hashfile = hashfile
        self.wordlist = wordlist
        for hashpsw in [line.strip() for line in open(hashfile)]:
            if len(hashpsw) == 34:
                self.WPCrackHashes()
                break
            elif len(hashpsw) == 65:
                self.JooCrackHashes()
                break
            else:
                msg = "No Valid Password Hash: " + hashpsw
                report.error(msg)

    # Run hashcat to crack WordPress hashes
    def WPCrackHashes(self):
        # hashcat -m 400 -a 0 -o cracked.txt hashes.txt passw.txt
        msg = "Cracking WordPress Hashes in: " + self.hashfile + " ... "
        report.verbose(msg)
        process = os.system("hashcat -m 400 -a 0 -o cracked.txt " +
                            self.hashfile + " " + self.wordlist)
        if process == 0:
            msg = "Cracked Passwords saved in: cracked.txt"
            report.info(msg)
        else:
            msg = "Cracking could not be completed. Please install hashcat: http://hashcat.net/"
            report.error(msg)

    # Run hashcat to crack Joomla hashes
    def JooCrackHashes(self):
        # hashcat -m 10 -a 0 -o cracked.txt hashes.txt passw.txt
        msg = "Cracking Joomla Hashes in: " + self.hashfile + " ... "
        report.verbose(msg)
        process = os.system("hashcat -m 10 -a 0 -o cracked.txt " + self.hashfile
                            + " " + self.wordlist)
        if process == 0:
            msg = "Cracked Passwords saved in: cracked.txt"
            report.info(msg)
        else:
            msg = "Cracking could not be completed. Please install hashcat: http://hashcat.net/"
            report.error(msg)


postexploiter = PostExploit()
